package com.bjsxt.synch1;

public class TicketRunnable implements Runnable {

    private int ticketNumber = 400;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (ticketNumber <= 0) {
                    break;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()
                        + "正在卖票，票号为：" + ticketNumber--);
            }

        }
    }

    public static void main(String[] args) {
        //模拟4个窗口卖票
        Runnable ticketRunnable = new TicketRunnable();

        Thread t1 = new Thread(ticketRunnable);
        Thread t2 = new Thread(ticketRunnable);
        Thread t3 = new Thread(ticketRunnable);
        Thread t4 = new Thread(ticketRunnable);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t4.setName("窗口4");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

